package entity

import (
	"fmt"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

//ShAsseZbDept
type ShAsseZbDept struct {
	ID           int          `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	ZBID         int          `gorm:"column:ZBID;"`
	ShConfZbDept ShConfZbDept `gorm:"ForeignKey:ZBID"`
	ZBMC         string       `gorm:"column:ZBMC;"`
	JQ           float32      `gorm:"column:JQ;"`
	ZL           float32      `gorm:"column:ZL;"`
	MBZ          float32      `gorm:"column:MBZ;"`
	UTID         int          `gorm:"column:UTID;"`
	UTNM         string       `gorm:"column:UTNM;"`
	NF           int          `gorm:"column:NF;"`
	ZL1          float32      `gorm:"column:ZL1"`
	MB1          float32      `gorm:"column:MB1"`
	ZL2          float32      `gorm:"column:ZL2"`
	MB2          float32      `gorm:"column:MB2"`
	ZL3          float32      `gorm:"column:ZL3"`
	MB3          float32      `gorm:"column:MB3"`
	ZL4          float32      `gorm:"column:ZL4"`
	MB4          float32      `gorm:"column:MB4"`
	ZL5          float32      `gorm:"column:ZL5"`
	MB5          float32      `gorm:"column:MB5"`
	ZL6          float32      `gorm:"column:ZL6"`
	MB6          float32      `gorm:"column:MB6"`
	ZL7          float32      `gorm:"column:ZL7"`
	MB7          float32      `gorm:"column:MB7"`
	ZL8          float32      `gorm:"column:ZL8"`
	MB8          float32      `gorm:"column:MB8"`
	ZL9          float32      `gorm:"column:ZL9"`
	MB9          float32      `gorm:"column:MB9"`
	ZL10         float32      `gorm:"column:ZL10"`
	MB10         float32      `gorm:"column:MB10"`
	ZL11         float32      `gorm:"column:ZL11"`
	MB11         float32      `gorm:"column:MB11"`
	ZL12         float32      `gorm:"column:ZL12"`
	MB12         float32      `gorm:"column:MB12"`
	ORDER        int          `gorm:"column:ORDER"`
}

//ShConfZbDept
type ShConfZbDept struct {
	ZBID  int    `gorm:"AUTO_INCREMENT;primary_key;column:ZBID"`
	ZBMC1 string `gorm:"column:ZBMC1"`
	ZBMC2 string `gorm:"column:ZBMC2"`
	ZBLX  string `gorm:"column:ZBLX"`
	NF    int    `gorm:"column:NF"`
	STNO  int    `gorm:"column:STNO"`
}

//TableName
func (ShAsseZbDept) TableName() string {
	return "SH_ASSE_ZB_DEPT"
}

//TableName
func (ShConfZbDept) TableName() string {
	return "SH_CONF_ZB_DEPT"
}

// BeforeCreate
func (s *ShAsseZbDept) BeforeCreate(tx *gorm.DB) (err error) {
	err = tx.Raw(`SELECT "ID" FROM "T_Organization" WHERE "OrganName"=?`, s.UTNM).Row().Scan(&s.UTID)
	if err != nil || s.UTID == 0 {
		err = fmt.Errorf("机构名称%s在系统中不存在！", s.UTNM)
		return
	}
	err = tx.Raw(`SELECT "ZBID","STNO" FROM "SH_CONF_ZB_DEPT" WHERE "NF"=? AND ("ZBMC1"=? OR "ZBMC2"=?)`, s.NF, s.ZBMC, s.ZBMC).Row().Scan(&s.ZBID, &s.ORDER)
	if err != nil || s.ZBID == 0 {
		err = fmt.Errorf("指标名称%s在系统中不存在！", s.ZBMC)
		return
	}
	return
}

//ShAsseZbDeptEF 返回结构体
func ShAsseZbDeptEF() base.EntityFunc {
	return func() interface{} {
		entity := ShAsseZbDept{}
		return &entity
	}
}

//ShAsseZbDeptESF 返回结构体数组
func ShAsseZbDeptESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShAsseZbDept
		return &entities
	}
}

//ShConfZbDeptEF 返回结构体
func ShConfZbDeptEF() base.EntityFunc {
	return func() interface{} {
		entity := ShConfZbDept{}
		return &entity
	}
}

//ShConfZbDeptESF 返回结构体数组
func ShConfZbDeptESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []ShConfZbDept
		return &entities
	}
}

func init() {
	relationship := []string{"ShConfZbDept"}
	base.DaoPoolObj.Put("ShAsseZbDept", base.NewDefaultDao(ShAsseZbDeptEF(), ShAsseZbDeptESF(), relationship, "my"))
	base.DaoPoolObj.Put("ShConfZbDept", base.NewDefaultDao(ShConfZbDeptEF(), ShConfZbDeptESF(), nil, "my"))
}
